package com.da.lon.wang.xlg.view;

import android.content.Context;
import android.text.Html;
import android.util.AttributeSet;
import android.widget.TextView;

public class TimerTextView extends TextView implements Runnable {

    private long mhour, mmin, msecond;// 天，小时，分钟，秒
    private boolean run = true; // 是否启动了

    public TimerTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setTimes(int[] times) {
        mhour = times[0];
        mmin = times[1];
        msecond = times[2];

    }

    /**
     * 倒计时计算
     */
    private void ComputeTime() {
        msecond--;
        if (msecond < 0) {
            mmin--;
            msecond = 59;
            if (mmin < 0) {
                mmin = 59;
                mhour--;
            }

        }

    }

    public boolean isRun() {
        return run;
    }

    public void beginRun() {
        this.run = true;
        run();
    }

    public void stopRun() {
        this.run = false;
    }

    @Override
    public void run() {
        // 标示已经启动
        if (run) {
            ComputeTime();

            this.setText(Html.fromHtml("<font color='#FF3E47'>" + mhour
                    + "</font>" + "<font color='#5e5e5e'>小时</font>"
                    + "<font color='#FF3E47'>" + mmin + "</font>"
                    + "<font color='#5e5e5e'>分钟</font>"
                    + "<font color='#FF3E47'>" + msecond + "</font>"
                    + "<font color='#5e5e5e'>秒</font>"));

            postDelayed(this, 1000);
        } else {
            removeCallbacks(this);
        }
    }

}